let AdCount = require("../dbServer/dbSync").AdCount
var fs = require('fs');
var path = require('path');
let assert = require("assert")

var readline = require('readline');
var fRead = fs.createReadStream(path.join(__dirname, './my.log'));

var objReadline = readline.createInterface({
	input: fRead,
});
function getRes() {
	let laster = {oid: -1, time: 0}
	let arr = []
	let treeMap = {}
	objReadline.on("line",
		/**
		 * @param {string} line 
		 */
		function (line) {

			if (line && line.length > 10) {
				let time = new Date(line.slice(0, line.indexOf(",")))
				let oid = Number(line.slice(line.lastIndexOf("#click") + 6))
				if (oid != laster.oid || time.getTime() - laster.time > 1000) {
					laster = {oid, time: time.getTime()}
				} else {
					// console.log(line)
					return
				}
				treeMap[time.toLocaleDateString()] = treeMap[time.toLocaleDateString()] || {}
				let toDay = treeMap[time.toLocaleDateString()]
				toDay[oid] = toDay[oid] || {adClick: 0, oid, date: time.getTime()}
				toDay[oid].adClick++
				// toDay[oid].push({time, oid, })

				// let oid = Number()
				// console.log(toDay[oid])
			}
			return
		})
	objReadline.on("close", () => {
		let allVal = []
		delete treeMap["11/30/2018"]
		Array.from(Object.keys(treeMap), (key) => {
			Object.keys(treeMap[key]).forEach((oid) => {
				allVal.push(treeMap[key][oid])
			})
		})
		async function update(allVal) {
			for (let i = 0; i < allVal.length; i++) {
				val = allVal[i]
				await AdCount.upsert(val, {
						fields:["adClick"]
				})

			}
		}
		update(allVal)
		// Object.prototype.

	})
}
getRes()